from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression


def preprocess_data(df, label_col='HeartDisease'):
    # 特征选择
    X = df.drop(columns=[label_col])
    y = df[label_col]

    # 使用递归特征消除 (RFE) 替代 SelectKBest
    estimator = LogisticRegression(solver='lbfgs', max_iter=1000)
    selector = RFE(estimator, n_features_to_select=15, step=1)
    X_new = selector.fit_transform(X, y)
    selected_features = X.columns[selector.support_]
    print(f"Selected features: {list(selected_features)}")

    # 特征缩放
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X_new)

    return X_scaled, y, selected_features  # 返回选择的特征
